package com.zl.system.controller;

import com.zl.basic.util.AjaxResult;
import com.zl.basic.util.PageList;
import com.zl.system.domain.DictionaryType;
import com.zl.system.query.DictionaryTypeQuery;
import com.zl.system.service.DictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class DictionaryTypeController {
    @Autowired
    private DictionaryTypeService dictionaryTypeService;
    /**
     * 查询一个对象的数据
     * */
    @GetMapping("/{id}")
    public DictionaryType findOne(@PathVariable("id") Long id) {
        return dictionaryTypeService.findById(id);
    }
    /**
     * 查询所有
     * */
    @GetMapping
    public List<DictionaryType> findAll() {
        return dictionaryTypeService.findAll();
    }
    /**
     * 分页查询
     * */
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery) {

        return dictionaryTypeService.queryPage(dictionaryTypeQuery);
    }

    /**
     * 删除操作
     * */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    /**
     * 添加或者修改操作
     * */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody  DictionaryType dictionaryType) {
        try {
            if (dictionaryType.getId()==null){
                dictionaryTypeService.save(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！！！");
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }
}
